package com.kangdi.aitranslate.service;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

@Service
public class TranslateService {

    @Autowired
    private DashScopeChatModel chatClient;

    public String translate(String sourceLanguage,String targetLanguage,String content){
        String format = String.format(" 你是一个java spring 学习者，将以下内容从 %s 精准翻译为 %s。\n" +
                "            要求：保留专业术语、数字、符号、表格和代码块格式。\n" +
                "            待翻译内容：%s",sourceLanguage,targetLanguage,content);
        String response = chatClient.call(format);
        if (response.contains("</think>")){
            response= response.substring(response.indexOf("</think>\n\n")+10);
        }
        return response;
    }

    public String translate(String roleInfo,String sourceLanguage,String targetLanguage,String content){
        String format = String.format(" %s，将以下内容从 %s 精准翻译为 %s。\n" +
                "            要求：保留专业术语、数字、符号、表格和代码块格式。\n" +
                "            待翻译内容：%s",roleInfo,sourceLanguage,targetLanguage,content);
        String response = chatClient.call(format);
        if (response.contains("</think>")){
            response= response.substring(response.indexOf("</think>\n\n")+10);
        }
        return response;
    }
}
